﻿using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.ExceptionManagement;
using Kiiro.Model;
using Kiiro.Model.TimerJobs;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using TimerJobConstants = Kiiro.Common.Utility.Constants.SPSiteDefinition.TimerJobs;

namespace Kiiro.UnitTests
{   
    /// <summary>
    ///This is a test class for ProjectsModelTest and is intended
    ///to contain all ProjectsModelTest Unit Tests
    ///</summary>
    [TestClass ( )]
    public class ProjectsModelTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for UpdateProjectStatistics
        ///</summary>
        [TestMethod ( )]
        public void UpdateProjectStatisticsTest ( )
        {
            SPSecurity.RunWithElevatedPrivileges ( delegate ( )
            {
                using ( SPSite site = new SPSite ( "http://localhost" ) )
                {
                    using ( SPSite systemSite = SPHelpers.GetSystemSite ( site.ID ) )
                    {
                        foreach ( SPWeb web in systemSite.AllWebs )
                        {
                            if ( SPHelpers.IsProject ( web ) )
                            {
                                try
                                {
                                    ProjectsModel.UpdateProjectStatistics ( new Midori ( ), site.ID, web.ServerRelativeUrl, true );
                                }
                                catch ( Exception ex )
                                {
                                    ExceptionManager.HandleException ( ex, false );
                                }
                            }
                        }
                    }
                }
            } );

            Assert.Inconclusive ( "A method that does not return a value cannot be verified." );
        }

        //[TestMethod ( )]
        //public void UpdateStatisticsJobSchedule ( )
        //{
        //    SPSecurity.RunWithElevatedPrivileges ( delegate ( )
        //    {
        //        using ( SPSite site = new SPSite ( "http://localhost" ) )
        //        {
        //            using ( SPSite elevatedSite = SPHelpers.GetSystemSite ( site.ID ) )
        //            {
        //                try
        //                {
        //                    string jobName = TimerJobConstants.StatisticsJobName + site.ID.ToString ( );

        //                    elevatedSite.WebApplication.JobDefinitions.Cast<SPJobDefinition> ( )
        //                        .Where ( x => x.Name == jobName || x.TypeName == TimerJobConstants.OldStatsJobType )
        //                        .ToList ( )
        //                        .ForEach ( delegate ( SPJobDefinition jobDefinition ) { jobDefinition.Delete ( ); } );

        //                    StatisticsTimerJob timerJob = new StatisticsTimerJob ( jobName, elevatedSite.WebApplication );
        //                    SPDailySchedule schedule = new SPDailySchedule { BeginHour = 1, EndHour = 2 };
        //                    //SPMinuteSchedule schedule = new SPMinuteSchedule { BeginSecond = 0, EndSecond = 59, Interval = 5 };
        //                    timerJob.Schedule = schedule;
        //                    SPSecurity.RunWithElevatedPrivileges ( delegate ( ) { timerJob.Update ( ); } );
        //                }
        //                catch ( Exception ex )
        //                {
        //                    ExceptionManagement.ExceptionManager.HandleException ( ex, false );
        //                }
        //            }
        //        }
        //    } );
        //}
    }
}
